/**
 * 
 */
package com.ideeli.webextractor.service;

import static com.ideeli.webextractor.utils.Constants.IMAGE_LARGE_PATH_GIGA;
import static com.ideeli.webextractor.utils.Constants.IMAGE_LARGE_PATH_MSI;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.ideeli.webextractor.dao.ProductDao;

/**
 * @author sa
 *
 */
public class CacheServiceImpl implements CacheService
{
	private static final Logger logger = Logger.getLogger(CacheServiceImpl.class);
			
	private Map<String,Integer> catNameId= new HashMap<String,Integer>();
	
	private Map<String,Integer> supplierNameId=new HashMap<String,Integer>();
	
	private Map<String,Integer> manufacturers= new HashMap<String,Integer>();
	
	private Map<String,Integer> allSkuVpns;
	
	private Map<String,Integer> parsedSkuVpns;
	
	private Map<String,String> ingHwzCatMapping;
	
	private Map<Integer,String> prodIDebCatCode;
	
	private Map<String,String> ebCatMapping;
	
	private List<String> errorList = new ArrayList<String>();
	
	private Properties appProperties = null;
	
	private boolean homeRun;
	
	private Map<String,String> updateProdPriceList = new HashMap<String,String>();
	
	private String actualImagePath=null;
	
//	private boolean isLoaded=false;
	
	private ProductDao productDao;
	
	private Map<String,Integer> categoryLimit=null;
	
	private int frontPageNo=0;
	private int backPageNo=100;
	
	private Map<String,String> configMap=new HashMap<String,String>();
	
	
	public CacheServiceImpl()  
	{
		
	}
	
	public int getCategoryId(String name) 
	{
		logger.debug("getCategoryId()");
	/*	if(!isLoaded)
		{
			logger.warn("loading cache");
			try {
				loadCache();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		
		Integer id=catNameId.get(name); 
		if(id==null)
			return -1;
		
		return id;
		
	}
	public int getSupplierId(String name) 
	{
		logger.debug("getSupplierId()");
		Integer id=supplierNameId.get(name); 
		if(id==null)
			return -1;
		
		return id;
		
	}
	
	/*
	private void loadCache() throws Exception{
		
		catNameId=productDao.loadCategories();
		
		isLoaded=true;
		
	}*/

	public void addCacheCatId(String name,int id)
	{
		catNameId.put(name,id);
	}
	
	public void addSupplierNameId(String name,int id)
	{
		supplierNameId.put(name,id);
	}

	
	
	public void addManufacturerCache(String name,int id)
	{
		manufacturers.put(name,id);
	}
	
	public int getManufacturerFrmCache(String name)
	{
		Integer id=manufacturers.get(name); 
		if(id==null)
			return -1;
		
		return id;
	}

	public ProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	/**
	 * @return the allSkuVpns
	 */
	public Map<String,Integer> getAllSkuVpns(int supplierid) {
		
		if(allSkuVpns==null)
		{
			try {
				allSkuVpns=productDao.loadAllSkuVpn("product p, product_to_supplier pts" +
						" where p.product_id=pts.product_id and pts.supplier_id="+supplierid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(allSkuVpns==null)
				allSkuVpns= new HashMap<String,Integer>();
		}
		
		return allSkuVpns;
	}
	
	/**
	 * @return the allSkuVpns
	 */
	public Map<String,Integer> getParsedSkuVpns(int supplierid) {
		
		if(parsedSkuVpns==null)
		{
			try {
				parsedSkuVpns=productDao.loadAllSkuVpn(" parse_history p, product_to_supplier pts" +
						" where p.ts > current_date-1 and p.product_id=pts.product_id and pts.supplier_id="+supplierid);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(parsedSkuVpns==null)
				parsedSkuVpns= new HashMap<String,Integer>();
		}
		
		return parsedSkuVpns;
	}
	

	public String getExtension(String vendor) 
	{

//		if(getJpgList().contains(vendor))
//		{
//			return ".jpg";
//		}
//		else 
		if(getPngList().contains(vendor))
		{
			return ".png";
		}
		else if(getGifList().contains(vendor))
		{
			return ".gif";
		}
		
		return ".jpg";
	}

	private List<String> getGifList() {
		
		if(gifList==null)
		{
			gifList= new ArrayList<String>();
			gifList.add("Cisco");
		}
		return gifList;
	}

	private List<String> getPngList() {
		if(pngList==null)
		{
			pngList=new ArrayList<String>();
			pngList.add("Juniper");
		}
		return pngList;
	}

/*	private List<String> getJpgList() {

		if(jpgList==null)
		{
			jpgList=new ArrayList<String>();
			jpgList.add("3m");
			jpgList.add("Acer");
			jpgList.add("Adobe");
			jpgList.add("Apc - Schneider");
			jpgList.add("Asus");
			jpgList.add("Autodesk");
			jpgList.add("Avocent");
			jpgList.add("Axis");
			jpgList.add("Brother");
			jpgList.add("Canon");
			jpgList.add("Corel");
			jpgList.add("Emerson");
			jpgList.add("Epicsoft");
			jpgList.add("Fujitsu");
			jpgList.add("Hp");
			jpgList.add("Ibm");
			jpgList.add("Imation");
			jpgList.add("Intel");
			jpgList.add("Kingston");
			jpgList.add("Lg");
			jpgList.add("Mcafee");
			jpgList.add("Microsoft");
			jpgList.add("Motorola");
			jpgList.add("Newell");
			jpgList.add("Nuance");
			jpgList.add("Polycom");
			jpgList.add("Samsung");
			jpgList.add("Sandisk");
			jpgList.add("Seagate");
			jpgList.add("Symantec");
			jpgList.add("Symbol");
			jpgList.add("Tomtom");
			jpgList.add("Ubisoft");
			jpgList.add("V7");
			jpgList.add("Zebra");
		}
		return jpgList;
	}*/
	
	private List<String> gifList=null;
	private List<String> pngList=null;
//	private List<String> jpgList=null;

	public Map<String, String> getUpdateProdPriceList()
	{
		return updateProdPriceList;
	}

	public void addUpdateProdPriceList(String productId, String newNOldPrice)
	{
		
		this.updateProdPriceList.put(productId, newNOldPrice);
	}

	public List<String> getErrorList()
	{
		return errorList;
	}

	public void addErrorToList(String errorText)
	{
		this.errorList.add(errorText);
	}

	public String getAppProperty(String name) throws Exception
	{
		if(appProperties==null)
		{
			appProperties=new Properties();
			appProperties.load(new FileInputStream("prop/app.properties"));
		}
		
		return appProperties.getProperty(name);
		
	}
	
	public void setAppProperty(String name,String value) throws Exception
	{
		if(appProperties==null)
		{
			appProperties=new Properties();
			appProperties.load(new FileInputStream("prop/app.properties"));
		}
		
		 appProperties.setProperty(name, value);
		
	}


	public String getActualImagePath()
	{
		if(this.actualImagePath==null)
		{
			if("sa".equals(System.getProperty("user.name")))
			{
				this.actualImagePath=IMAGE_LARGE_PATH_MSI;
			}
			else
			{
				this.actualImagePath=IMAGE_LARGE_PATH_GIGA;
			}
			
		}
		
		return this.actualImagePath;
	}


	public void setActualImagePath(String actualImagePath)
	{
		this.actualImagePath = actualImagePath;
	}

	public String getIngHwzCatMapping(String ingCategory) {
		
		String hwzCategory="";
		if(ingHwzCatMapping==null)
		{
			ingHwzCatMapping= new HashMap<String,String>();
			
			ingHwzCatMapping.put("A/v & Music Accessories","Portable Media Players");
			ingHwzCatMapping.put("A/v Cables & Adapters","Networking");
			ingHwzCatMapping.put("Accessories & Components","Full & Partial Systems");
			ingHwzCatMapping.put("Aidc Mobile Terminals","Others");
			ingHwzCatMapping.put("Aidc Wireless Infrastructure","Others");
			ingHwzCatMapping.put("Audio/visual","Home Entertainment");
			ingHwzCatMapping.put("Auto Id Accessories","Others");
			ingHwzCatMapping.put("Auto Id Barcode Scanners","Others");
			ingHwzCatMapping.put("Auto Id Printers","Office Equipment");
			ingHwzCatMapping.put("Backup Devices","Storage");
			ingHwzCatMapping.put("Business Solution Software","Software");
			ingHwzCatMapping.put("Cables","Networking");
			ingHwzCatMapping.put("Cameras","Digital Cameras & Video Camcorders");
			ingHwzCatMapping.put("Cameras & Scanners","Digital Cameras & Video Camcorders");
			ingHwzCatMapping.put("Communications","Others");
			ingHwzCatMapping.put("Communications Software","Software");
			ingHwzCatMapping.put("Components","Full & Partial Systems");
			ingHwzCatMapping.put("Computer Cases & Accessories","Full & Partial Systems");
			ingHwzCatMapping.put("Computers","Full & Partial Systems");
			ingHwzCatMapping.put("Connectivity Devices","Networking");
			ingHwzCatMapping.put("Content Networking","Networking");
			ingHwzCatMapping.put("Data Management Software","Software");
			ingHwzCatMapping.put("Desktops","Full & Partial Systems");
			ingHwzCatMapping.put("Developer Software","Software");
			ingHwzCatMapping.put("Digital Audio/visual Devices","Home Entertainment");
			ingHwzCatMapping.put("Digital Imaging & Signage Software","Software");
			ingHwzCatMapping.put("Displays","Display & Graphics Card");
			ingHwzCatMapping.put("Drive Controllers","Mainboards");
			ingHwzCatMapping.put("Flash Memory Devices","Memory, Flash Drives & Accessories");
			ingHwzCatMapping.put("Games","Software");
			ingHwzCatMapping.put("Gaming","Software");
			ingHwzCatMapping.put("Gaming Accessories","Home Entertainment");
			ingHwzCatMapping.put("Gaming Consoles","Home Entertainment");
			ingHwzCatMapping.put("Hard Drives","Storage");
			ingHwzCatMapping.put("I/o Device Cables","Full & Partial Systems");
			ingHwzCatMapping.put("Inkjet","Office Equipment");
			ingHwzCatMapping.put("Input Devices","Others");
			ingHwzCatMapping.put("It Management Software","Software");
			ingHwzCatMapping.put("Keyboards & Keypads","Others");
			ingHwzCatMapping.put("Laser","Office Equipment");
			ingHwzCatMapping.put("Managed Services","Others");
			ingHwzCatMapping.put("Media & Cleaning Cartridges","Office Equipment");
			ingHwzCatMapping.put("Memory","Memory, Flash Drives & Accessories");
			ingHwzCatMapping.put("Mice/pointers & Graphic Tables","Others");
			ingHwzCatMapping.put("Mobility Accessories","Others");
			ingHwzCatMapping.put("Mobility Devices","Others");
			ingHwzCatMapping.put("Modems & Connectivity Adapters","Networking");
			ingHwzCatMapping.put("Monitor/tv Accessories","Display & Graphics Card");
			ingHwzCatMapping.put("Monitors","Display & Graphics Card");
			ingHwzCatMapping.put("Motherboards","Mainboards");
			ingHwzCatMapping.put("Network Attached Storage (nas)","Storage");
			ingHwzCatMapping.put("Network Devices","Networking");
			ingHwzCatMapping.put("Network Interface Cards","Networking");
			ingHwzCatMapping.put("Network Security","Networking");
			ingHwzCatMapping.put("Networking","Networking");
			ingHwzCatMapping.put("Notebook/tablet Accessories","Notebooks & Accessories");
			ingHwzCatMapping.put("Notebooks & Tablets","Notebooks & Accessories");
			ingHwzCatMapping.put("Office Electronics & Furniture","Office Equipment");
			ingHwzCatMapping.put("Office Equipment","Office Equipment");
			ingHwzCatMapping.put("Office Productivity Software","Software");
			ingHwzCatMapping.put("Office Supplies","Office Equipment");
			ingHwzCatMapping.put("Operating System Software","Software");
			ingHwzCatMapping.put("Optical Drives","Optical Storage");
			ingHwzCatMapping.put("Phone Accessories","Mobile Phones & Accessories");
			ingHwzCatMapping.put("Phone Systems; Phones & Components","Mobile Phones & Accessories");
			ingHwzCatMapping.put("Physical Access Control","Others");
			ingHwzCatMapping.put("Physical Security","Others");
			ingHwzCatMapping.put("Pos Peripherals","Others");
			ingHwzCatMapping.put("Pos/aidc/barcode Products","Office Equipment");
			ingHwzCatMapping.put("Power & Rack Equipment","Server & Workstation");
			ingHwzCatMapping.put("Power Cables","Others");
			ingHwzCatMapping.put("Power Equipment","Others");
			ingHwzCatMapping.put("Printer/plotter Accessories","Office Equipment");
			ingHwzCatMapping.put("Printer/plotter Supplies","Office Equipment");
			ingHwzCatMapping.put("Printers","Office Equipment");
			ingHwzCatMapping.put("Processors","CPU");
			ingHwzCatMapping.put("Projector Accessories","Office Equipment");
			ingHwzCatMapping.put("Projectors","Office Equipment");
			ingHwzCatMapping.put("Racks & Enclosures","Others");
			ingHwzCatMapping.put("Routers & Components","Networking");
			ingHwzCatMapping.put("San Systems & Infrastructure","Others");
			ingHwzCatMapping.put("Scanners","Office Equipment");
			ingHwzCatMapping.put("Security Software","Software");
			ingHwzCatMapping.put("Servers","Server & Workstation");
			ingHwzCatMapping.put("Services & Warranties","Others");
			ingHwzCatMapping.put("Software","Software");
			ingHwzCatMapping.put("Solid State Drives","Storage");
			ingHwzCatMapping.put("Specialty Printers","Office Equipment");
			ingHwzCatMapping.put("Storage","Storage");
			ingHwzCatMapping.put("Storage Accessories & Components","Storage");
			ingHwzCatMapping.put("Storage Cables","Storage");
			ingHwzCatMapping.put("Storage Software","Software");
			ingHwzCatMapping.put("Supplies & Media","Others");
			ingHwzCatMapping.put("Supplies & Media","Others");
			ingHwzCatMapping.put("Switches & Hubs","Networking");
			ingHwzCatMapping.put("Switching Devices","Networking");
			ingHwzCatMapping.put("Televisions","Home Entertainment");
			ingHwzCatMapping.put("Touch Screen Displays","Display & Graphics Card");
			ingHwzCatMapping.put("Transceivers & Converters","Others");
			ingHwzCatMapping.put("Tv Tuners & Video Editing","Others");
			ingHwzCatMapping.put("Video & Audio Conferencing Equipment","Office Equipment");
			ingHwzCatMapping.put("Video Gaming Accessories","Home Entertainment");
			ingHwzCatMapping.put("Voice Over Ip (voip)","Office Equipment");
			ingHwzCatMapping.put("Warranties","Others");
			ingHwzCatMapping.put("Warranties; Service Fees & Contracts","Others");
			ingHwzCatMapping.put("Whiteboards & Presentation Devices","Office Equipment");
			ingHwzCatMapping.put("Wireless Aidc Accessories","Networking");
			ingHwzCatMapping.put("Wireless Networking","Networking");
			
		}
		
		hwzCategory=ingHwzCatMapping.get(ingCategory);
		
		return hwzCategory == null ? "Others" : hwzCategory;
	}

	@Override
	public Map<String, Integer> getCategoryUpperLimit() 
	{
		if(categoryLimit==null)
		{
			categoryLimit= new HashMap<String,Integer>();
			
			categoryLimit.put("279",5);
			categoryLimit.put("286",5);
			categoryLimit.put("295",5);
			categoryLimit.put("1",5);
			categoryLimit.put("253",5);
			categoryLimit.put("105",5);
			categoryLimit.put("173",10);
			categoryLimit.put("277",10);
			categoryLimit.put("271",10);
			categoryLimit.put("172",15);
			categoryLimit.put("239",15);
			categoryLimit.put("289",15);
			categoryLimit.put("27",15);
			categoryLimit.put("258",1000);
			categoryLimit.put("259",1000);
			categoryLimit.put("256",1000);
			categoryLimit.put("257",1000);
			categoryLimit.put("248",1000);
			categoryLimit.put("247",1000);
			categoryLimit.put("241",1000);
			categoryLimit.put("176",1000);
			categoryLimit.put("178",1000);
			categoryLimit.put("225",1000);
			categoryLimit.put("221",1000);
			categoryLimit.put("218",1000);
		}
		return categoryLimit;
	}
	
	//TODO Map below with ebCateogryCodes
	public String getEbCategoryCode(String ingCategory) {
		
		String ebCategoryCode="";
		if(ebCatMapping==null)
		{
			ebCatMapping= new HashMap<String,String>();
			
			ebCatMapping.put("A/v & Music Accessories","32865");//not perfect
			ebCatMapping.put("A/v Cables & Adapters","3668");
			ebCatMapping.put("Accessories & Components","16145");
			ebCatMapping.put("Aidc Mobile Terminals","177");
			ebCatMapping.put("Aidc Wireless Infrastructure","44996");
			ebCatMapping.put("Audio/visual","50600");
			ebCatMapping.put("Auto Id Accessories","177");
			ebCatMapping.put("Auto Id Barcode Scanners","99277");
			ebCatMapping.put("Auto Id Printers","99277");
			
			ebCatMapping.put("Business Solution Software","182");
			ebCatMapping.put("Cables","11175");
			ebCatMapping.put("Cameras","3332");
			ebCatMapping.put("Cameras & Scanners","3332");
			ebCatMapping.put("Communications","61816");
			ebCatMapping.put("Communications Software","182");
			ebCatMapping.put("Components","16145");
			ebCatMapping.put("Computer Cases & Accessories","1244");
			ebCatMapping.put("Computers","51147");
			ebCatMapping.put("Connectivity Devices","3706");
			ebCatMapping.put("Content Networking","11175");
			ebCatMapping.put("Data Management Software","182");
			ebCatMapping.put("Desktops","51147");
			ebCatMapping.put("Developer Software","182");
			ebCatMapping.put("Digital Audio/visual Devices","32865");
			ebCatMapping.put("Digital Imaging & Signage Software","182");
			ebCatMapping.put("Displays","174");
			ebCatMapping.put("Drive Controllers","3753");
		
			ebCatMapping.put("Games","41871");
			ebCatMapping.put("Gaming","41871");
			ebCatMapping.put("Gaming Accessories","170");
			ebCatMapping.put("Gaming Consoles","170");
		
			ebCatMapping.put("I/o Device Cables","3668");
			ebCatMapping.put("Inkjet","16185");
			ebCatMapping.put("Input Devices","4706");
			ebCatMapping.put("It Management Software","182");
			ebCatMapping.put("Keyboards & Keypads","4706");
			ebCatMapping.put("Laser","3750");
			ebCatMapping.put("Managed Services","177");
			ebCatMapping.put("Media & Cleaning Cartridges","99277");
			ebCatMapping.put("Memory","51074");
			ebCatMapping.put("Mice/pointers & Graphic Tables","23160");
			ebCatMapping.put("Mobility Accessories","31534");
			ebCatMapping.put("Mobility Devices","31534");
			ebCatMapping.put("Modems & Connectivity Adapters","45003");
			ebCatMapping.put("Monitor/tv Accessories","174");
			ebCatMapping.put("Monitors","174");
			ebCatMapping.put("Motherboards","1244");
			
			ebCatMapping.put("Network Devices","11175");
			ebCatMapping.put("Network Interface Cards","45003");
			ebCatMapping.put("Network Security","11175");
			ebCatMapping.put("Networking","11175");
			ebCatMapping.put("Notebook/tablet Accessories","31534");
			ebCatMapping.put("Office Productivity Software","80267");

			ebCatMapping.put("Operating System Software","80267");
			ebCatMapping.put("Optical Drives","44955");
			ebCatMapping.put("Phone Accessories","20369");
			ebCatMapping.put("Phone Systems; Phones & Components","20369");
			ebCatMapping.put("Pos Peripherals","177");

			ebCatMapping.put("Power & Rack Equipment","177");
			ebCatMapping.put("Power Cables","3668");
			ebCatMapping.put("Power Equipment","177");
			ebCatMapping.put("Printer/plotter Accessories","99277");
			ebCatMapping.put("Printer/plotter Supplies","99277");
			ebCatMapping.put("Printers","3303");
			ebCatMapping.put("Processors","1244");
			ebCatMapping.put("Projector Accessories","174");
			ebCatMapping.put("Projectors","174");
			ebCatMapping.put("Racks & Enclosures","177");
			ebCatMapping.put("Routers & Components","44997");

			ebCatMapping.put("Scanners","3303");
			ebCatMapping.put("Security Software","182");
			ebCatMapping.put("Servers","51147");
			ebCatMapping.put("Software","182");
			

			ebCatMapping.put("Specialty Printers","99277");
			ebCatMapping.put("Supplies & Media","3754");
			ebCatMapping.put("Switches & Hubs","44997");
			ebCatMapping.put("Switching Devices","3706");
			ebCatMapping.put("Televisions","50600");
			ebCatMapping.put("Touch Screen Displays","174");
			ebCatMapping.put("Transceivers & Converters","177");
			ebCatMapping.put("Tv Tuners & Video Editing","101205");

			ebCatMapping.put("Video Gaming Accessories","3762");


			ebCatMapping.put("Wireless Aidc Accessories","61816");
			ebCatMapping.put("Wireless Networking","44996");
			
			
			//TODO find eb category
			ebCatMapping.put("Storage","167");
			ebCatMapping.put("Storage Accessories & Components","167");
			ebCatMapping.put("Storage Cables","167");
			ebCatMapping.put("Storage Software","182");

			ebCatMapping.put("Solid State Drives","167");
			ebCatMapping.put("Backup Devices","167");
			ebCatMapping.put("Hard Drives","56084");
			ebCatMapping.put("Flash Memory Devices","51074");
			ebCatMapping.put("Network Attached Storage (nas)","167");
			ebCatMapping.put("Notebooks & Tablets","80208");
			ebCatMapping.put("Office Electronics & Furniture","16145");
			ebCatMapping.put("Office Equipment","16145");
			ebCatMapping.put("Office Supplies","16145");
			ebCatMapping.put("Physical Access Control","177");//top level generic category Computer & Networking
			ebCatMapping.put("Physical Security","177");//top level generic category Computer & Networking
			ebCatMapping.put("Pos/aidc/barcode Products","177");//top level generic category Computer & Networking
			ebCatMapping.put("San Systems & Infrastructure","177");
			ebCatMapping.put("Services & Warranties","177");
			ebCatMapping.put("Warranties","177");
			ebCatMapping.put("Warranties; Service Fees & Contracts","177");

			ebCatMapping.put("Whiteboards & Presentation Devices","177");
			ebCatMapping.put("Video & Audio Conferencing Equipment","177");
			ebCatMapping.put("Voice Over Ip (voip)","177");
		}
		
		ebCategoryCode=ebCatMapping.get(ingCategory);
		
		return ebCategoryCode == null ? "177" : ebCategoryCode;
	}

	public String getProdIDebCatCode(Integer prodId) {
		if(this.prodIDebCatCode==null)
		{
			return null;
		}
		
		return this.prodIDebCatCode.get(prodId);
	}

	public void putProdIDebCatCode(Integer prodId,String ebCatCode ) {
		if(this.prodIDebCatCode==null)
		{
			this.prodIDebCatCode=new HashMap<Integer,String>();
		}
		
		this.prodIDebCatCode.put(prodId, ebCatCode);
		
	}

	@Override
	public int getFrontPageNo() {
		
		return frontPageNo;
	}

	@Override
	public void UpdateBackPageNo(int currentPageNo) {
		this.backPageNo=currentPageNo;
		
	}

	@Override
	public int getBackPageNo() {
		return backPageNo;
	}

	@Override
	public void updateFrontPgNo(int currentPageNo) {
		this.frontPageNo=currentPageNo;
		
	}
	
	public String getConfigMapValue(String key)
	{
		String value=configMap.get(key);
		if(value==null)
			value="";
		
		return value;
	}


	public Map<String, String> getConfigMap()
	{
		return configMap;
	}

	public void addConfigMap(String key, String val)
	{
		configMap.put(key, val);
	}

	

}
